#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include<solution.h>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setLayout(ui->gridLayout);
    ui->tableWidget->setRowCount(9);
    ui->tableWidget->setColumnCount(9);
    qDebug()<<ui->tableWidget->rowCount()<<"   "<<ui->tableWidget->columnCount()<<endl;
    setTableColor();

}

Widget::~Widget()
{
    delete ui;
}

QVector<QVector<int>> Widget::parse2DVector(QString &input)
{
    input.replace(QRegExp("[\"]"), "");
    QVector<QVector<int>> result;
    QString currentRow;
    bool inArray = false; // 用于跟踪我们是否在处理一个数组内

    for (QChar c : input) {
        if (c == '[') {
            inArray = true;
            currentRow.clear(); // 开始新的一行
        } else if (c == ']') {
            inArray = false;
            if (!currentRow.isEmpty()) {
                // 解析当前行并添加到结果中
                QStringList elements = currentRow.split(",", Qt::SkipEmptyParts);
                QVector<int> row;
                for (const QString &element : elements) {
                    bool ok;
                    int value = element.toInt(&ok);
                    if (ok) {
                        row.append(value);
                    } else {
                        // 处理非整数元素，这里可以决定是跳过、记录错误还是使用默认值
                        row.append(0); // 示例：使用0作为默认值
                    }
                }
                result.append(row);
                currentRow.clear(); // 准备处理下一行
            }
        } else if (inArray && c != ',') {
            currentRow.append(c);
            currentRow.append(',');// 如果在数组内且不是逗号，添加到当前行
        }
    }

    // 检查是否还有未处理的行（例如，如果输入字符串没有以']'结尾）
    if (!currentRow.isEmpty()) {
        // 解析最后一行并添加到结果中（与上面的代码相同）
    }

    return result;
}

void Widget::setTableColor()
{
    for(int i = 0 ; i < 3;i++)
    {
        for(int j = 3 ;j<6;j++)
        {
            QTableWidgetItem *item = ui->tableWidget->item(i, j);
            if (item == nullptr) {
                item = new QTableWidgetItem();
                ui->tableWidget->setItem(i, j, item);
            }

            item->setBackground(QBrush(Qt::gray));
        }
    }
    for(int i = 3; i < 6; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            QTableWidgetItem *item = ui->tableWidget->item(i, j);
            if (item == nullptr) {
                item = new QTableWidgetItem();
                ui->tableWidget->setItem(i, j, item);
            }

            item->setBackground(QBrush(Qt::gray));
        }
    }
    for(int i = 3; i < 6; i++)
    {
        for(int j = 6; j < 9; j++)
        {
            QTableWidgetItem *item = ui->tableWidget->item(i, j);
            if (item == nullptr) {
                item = new QTableWidgetItem();
                ui->tableWidget->setItem(i, j, item);
            }

            item->setBackground(QBrush(Qt::gray));
        }
    }
    for(int i = 6 ; i < 9;i++)
    {
        for(int j = 3 ;j<6;j++)
        {
            QTableWidgetItem *item = ui->tableWidget->item(i, j);
            if (item == nullptr) {
                item = new QTableWidgetItem();
                ui->tableWidget->setItem(i, j, item);
            }

            item->setBackground(QBrush(Qt::gray));
        }
    }
}


void Widget::on_pushButton_shurukuang_clicked()
{
    QString input = ui->textEdit_input->toPlainText();
    if(input.isEmpty()) return;
    auto out = parse2DVector(input);
    qDebug()<<out<<endl;
    qDebug()<<Solution().isValidSudoku(out);
    QString ret;
    ui->lineEdit->setText(Solution().isValidSudoku(out) ? "输入数独有效":"输入数度无效");
}

void Widget::on_pushButton_biaoge_clicked()
{
    QVector<QVector<int>> input;
    for(int i =0; i < 9;i++)
    {
        QVector<int> cnt;
        for(int j = 0; j < 9; j++)
        {
            QTableWidgetItem *item = ui->tableWidget->item(i, j);
            if (item == nullptr) return;
            ui->tableWidget->item(i,j)->text() == "" ? cnt.push_back(0):cnt.push_back(ui->tableWidget->item(i,j)->text().toInt());
        }
        input.push_back(cnt);
        cnt.clear();
    }
    QString ret;
    ui->lineEdit->setText(Solution().isValidSudoku(input) ? "输入数独有效":"输入数度无效");
}

void Widget::on_pushButton_clicked()
{
    QString input = ui->textEdit_input->toPlainText();

    if(input.isEmpty()) return;
    auto out = parse2DVector(input);
    for (int i = 0; i < 9; i++) {
        for(int j = 0; j < 9; j++)
        {
            QString cnt = out[i][j] == 0 ? "" : QString::number(out[i][j]);
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(cnt));
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignCenter);
        }
    }
    setTableColor();
}

void Widget::on_pushButton_qingkong_clicked()
{
    ui->lineEdit->clear();
    ui->textEdit_input->clear();
}

void Widget::on_pushButton_qingkongbiao_clicked()
{
    for(int i = 0; i < 9; i++)
    {
        for(int j = 0 ; j < 9;j++)
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(""));
        }
    }
    ui->lineEdit->clear();
    setTableColor();
}

void Widget::on_pushButton_tuichu_clicked()
{
    this->close();
}

void Widget::on_tableWidget_itemChanged(QTableWidgetItem *item)
{
    setTableColor();
}
